#ifndef FKDRAWMODEL_H
#define FKDRAWMODEL_H

#include <QObject>

#include <QVector>
#include <QSet>
#include <QMutex>
#include <QMutexLocker>
//#include <QOpenGLFunctions>

#include "fkglmodule_global.h"

class FKDisplayReferent;
class QOpenGLShaderProgram;
class QOpenGLBuffer;
//class QOpenGLVertexArrayObject;
//class FKDrawObject;

class FKGLMODULESHARED_EXPORT FKDrawModel:public QObject{
    Q_OBJECT
public:
    FKDrawModel(QObject* parent=0);
    ~FKDrawModel();
    void addReferent(FKDisplayReferent* obj){
        QMutexLocker(&(this->_mutex));
        _objects.insert(obj);}
    void removeReferent(FKDisplayReferent* obj);
    QString modelName() const{return _modelName;}
    const QSet<FKDisplayReferent*> objects() const{return _objects;}
    bool drawNextObject(const QMatrix4x4 &pMat, const QMatrix4x4 &vMat);
    bool pickNextObject(const QMatrix4x4 &pMat, const QMatrix4x4 &vMat);
    bool loadModel(QString modelName, QString path);
    bool initModel();
    QOpenGLShaderProgram* program() const{return _program;}
    QOpenGLShaderProgram* pickingProgram() const{return _pickingProgram;}
protected:
    QVector<QVector3D> _vertices;
    QVector<unsigned int> _indexes;
    QVector<QVector3D> _normals;
    QVector<QVector2D> _texCoords;
    QVector<QVector3D> _colors;
    int _verticeCount;
    int _indexCount;

    QOpenGLShaderProgram* _program;
    QOpenGLShaderProgram* _pickingProgram;
//    QOpenGLVertexArrayObject* _vao;

    QOpenGLBuffer* _vertexBuffer;
    QOpenGLBuffer* _indexBuffer;
    QOpenGLBuffer* _normalBuffer;
    QOpenGLBuffer* _colorBuffer;
    QOpenGLBuffer* _texBuffer;
private:
    virtual bool modelInitialization();
    virtual bool modelLoading(QString modelName, QString path);
    virtual void drawObject(FKDisplayReferent* obj, const QMatrix4x4& pMat, const QMatrix4x4& vMat);
    virtual void pickObject(FKDisplayReferent* obj, const QMatrix4x4& pMat, const QMatrix4x4& vMat);
    bool _loaded;
    bool _initialized;
    QString _modelName;
    QSet<FKDisplayReferent*> _objects;

    QSet<FKDisplayReferent*>::const_iterator _curObj;

    QMutex _mutex;
};

#endif // FKDRAWMODEL_H
